package com.mobile.challenge.ui.report.unit.admin

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.mobile.challenge.api.AdminMonthDataService
import com.mobile.challenge.bean.AdminWeekMonthDataBean
import com.mobile.challenge.ui.report.model.PeriodType
import com.mobile.challenge.user.UserHelper
import com.mobile.challenge.util.DateUtil
import com.mobile.challenge.util.SUCCESS_ERROR_CODE
import com.mobile.challenge.util.SUCCESS_ERROR_MSG
import com.mobile.frame.mvvmframe.base.BaseModel
import com.mobile.frame.mvvmframe.base.DataViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.*
import javax.inject.Inject

class AdminUnitReportViewModel @Inject constructor(application: Application, model: BaseModel?) : DataViewModel(application, model) {

    var dataList = MutableLiveData<List<AdminWeekMonthDataBean>>()


    fun getAdminData(calendar: Calendar, periodType: Int) {
        val formatDateStr = if (periodType == PeriodType.WEEKLY) {
            DateUtil.getFormatWeekStartData(calendar)
        } else {
            DateUtil.getFormatMonthStartData(calendar)
        }
        val subscribe = getRetrofitService(AdminMonthDataService::class.java)
                .getData(
                        UserHelper.getInstance().getUserId(),
                        formatDateStr,
                        DateUtil.getFormatDateStr(calendar)
                ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ response ->
                    if (response.errorCode == SUCCESS_ERROR_CODE && response.errorMsg == SUCCESS_ERROR_MSG) {
                        val data = response.data
                        AdminUtils.getInstance().setList(data)
                        dataList.value = data
                    }
                }, {

                })
    }


}